package com.nexgo.payment.trans.setting.terminalkey.dowmtmk;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.nexgo.payment.R;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.fragment.ProgressDialogFragment;
import com.nexgo.payment.ui.BaseActivity;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.TitleBuilder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wzp on 2018/3/24.
 */

public class TmkDownloadActivity extends BaseActivity implements TmkDownloadContract.View{
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    private ProgressDialogFragment mWaitDialogFragment;
    private TmkDownloadContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bcc_download_key);
        ButterKnife.bind(this);

        initTitle();

        new TmkDownloadPresenter(getApplicationContext(), this, Repository.getInstance());
    }

    /**
     * 初始化标题
     */
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setTitleText(getString(R.string.download_master_key))
                .build();
    }


    @OnClick({R.id.title_iv_left, R.id.btn_cancel,R.id.btn_confirm})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()){
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.btn_confirm:
                if (mPresenter != null) mPresenter.start();
                break;
            default:
                break;
        }
    }

    @Override
    public void showSuccessful() {
        tvDesc.setText(getString(R.string.import_key_success));
        ToastUtils.showToast(R.string.import_key_success);
        finish();
    }

    @Override
    public void showFail() {
        tvDesc.setText(getString(R.string.import_key_fail));
        ToastUtils.showToast(R.string.import_key_fail);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.destroy();
        }
    }
    @Override
    public void setPresenter(TmkDownloadContract.Presenter presenter) {
        mPresenter = presenter;
    }
    @Override
    public void setWaitingIndicator(boolean active, String tipTitle, String tip) {
        if (active) {
            if (mWaitDialogFragment == null) {
                mWaitDialogFragment = new ProgressDialogFragment();
            }
            mWaitDialogFragment.setTip(tip);
            mWaitDialogFragment.setTipTitle(tipTitle);
            mWaitDialogFragment.setCancelable(false);
            mWaitDialogFragment.show(getSupportFragmentManager(), null);
        } else {
            if (mWaitDialogFragment != null) {
                mWaitDialogFragment.dismiss();
                mWaitDialogFragment = null;
            }
        }
    }
}
